package Form;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JPasswordField;

import Class.ConnectDatabase;
import Class.MD5;
import Class.Procedure;
import Support.International;
import Support.Mail;

import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Login {

	//flag for language
	public static int flgLanguage; // 0: English; 1: Vietnamese
	
	//security
	private static JTextField tfLoginName;
	private static JPasswordField pfLoginPassword;
	
	public static JTextField getLoginName()
	{
		return tfLoginName;
	}
	public static JPasswordField getLoginPassword()
	{
		return pfLoginPassword;
	}
	
	//public labels for multi-language
	public static JFrame frLogin;
	public static JLabel lbWarning;
	public static JLabel lbLoginName;
	public static JLabel lbPassword;
	public static JLabel lbLogin;
	public static JButton btnLogin;
	public static JButton btnCancel;
	public static JButton btnForgotPassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.frLogin.setVisible(true);
					//Login.frLogin.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
		ConnectDatabase.connection();
		International.locale = new Locale("en","UK");
		International.setLanguage(International.locale);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frLogin = new JFrame();
		frLogin.setBounds(400, 250, 355, 255);
		frLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frLogin.getContentPane().setLayout(null);
		
		tfLoginName = new JTextField();
		tfLoginName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if(evt.getKeyCode() == KeyEvent.VK_ENTER)
				{
					Procedure.checkLogin(tfLoginName.getText(), String.valueOf(pfLoginPassword.getPassword()));
				}
			}
		});
		tfLoginName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		tfLoginName.setBounds(157, 53, 132, 23);
		frLogin.getContentPane().add(tfLoginName);
		tfLoginName.setColumns(10);
		
		lbLoginName = new JLabel("Login name :");
		lbLoginName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lbLoginName.setBounds(29, 55, 128, 19);
		frLogin.getContentPane().add(lbLoginName);
		
		lbPassword = new JLabel("Password :");
		lbPassword.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lbPassword.setBounds(29, 85, 107, 19);
		frLogin.getContentPane().add(lbPassword);
		
		lbLogin = new JLabel("   LOGIN");
		lbLogin.setFont(new Font("Tahoma", Font.BOLD, 20));
		lbLogin.setBounds(99, 8, 144, 34);
		frLogin.getContentPane().add(lbLogin);
		
		lbWarning = new JLabel("Invalid login name or password !");
		lbWarning.setForeground(Color.RED);
		lbWarning.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lbWarning.setBounds(29, 146, 260, 19);
		frLogin.getContentPane().add(lbWarning);
		lbWarning.setVisible(false);
		
		pfLoginPassword = new JPasswordField();
		pfLoginPassword.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if(evt.getKeyCode() == KeyEvent.VK_ENTER)
				{
					Procedure.checkLogin(tfLoginName.getText(), String.valueOf(pfLoginPassword.getPassword()));
				}
			}
		});
		pfLoginPassword.setBounds(157, 85, 132, 23);
		frLogin.getContentPane().add(pfLoginPassword);
		
		btnLogin = new JButton("OK");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				Procedure.checkLogin(tfLoginName.getText(), String.valueOf(pfLoginPassword.getPassword()));
				
			}
		});
		btnLogin.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnLogin.setBounds(46, 178, 101, 23);
		frLogin.getContentPane().add(btnLogin);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);
			}
		});
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnCancel.setBounds(209, 176, 80, 23);
		frLogin.getContentPane().add(btnCancel);
		
		JButton btnVN = new JButton("");
		btnVN.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				International.locale = new Locale("vi","VN");
				International.setLanguage(International.locale);
				flgLanguage = 1;
			}
		});
		ImageIcon iconVN = new ImageIcon("images/VN.png");
		btnVN.setIcon(iconVN);
		btnVN.setBounds(265, 11, 29, 23);
		frLogin.getContentPane().add(btnVN);
		
		JButton btnUK = new JButton("");
		btnUK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				International.locale = new Locale("en","UK");
				International.setLanguage(International.locale);
				flgLanguage = 0;
			}
		});
		ImageIcon iconUK = new ImageIcon("images/UK.png");
		btnUK.setIcon(iconUK);
		btnUK.setBounds(301, 11, 29, 23);
		frLogin.getContentPane().add(btnUK);
		
		btnForgotPassword = new JButton("Forgot password ?");
		btnForgotPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				new ForgotPassword();
			}
		});
		btnForgotPassword.setForeground(Color.BLUE);
		btnForgotPassword.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnForgotPassword.setBounds(23, 115, 152, 23);
		frLogin.getContentPane().add(btnForgotPassword);
	}
}
